﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using System.Windows.Data;
using SMS.Model;
using System.ComponentModel;
using SMS.Common.Silverlight;

namespace SMS.ViewModel.AppHealth
{
    public class AppHealthViewModel : BaseNotify
    {


        AppHealthModel _model;
        public AppHealthViewModel()
        {
            if (App.IsRunning)
            {
                _model = new AppHealthModel();
                _model.OnServiceError += new Action<Entities.SMSException>(_model_OnServiceError);
                _model.OnGetUserActivityCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.UserActivity>>(_model_OnGetUserActivityCompleted);
                _model.OnGetLoginHistoryCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.LoginHistory>>(_model_OnGetLoginHistoryCompleted);
                _model.OnGetExceptionHistoryCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.ExceptionLog>>(_model_OnGetExceptionHistoryCompleted);
                ShowCommand = new DelegateCommand(ShowUserActivityCommand, null);
                FromDate = DateTime.Now.AddDays(-1);
                ToDate = DateTime.Now;
            }
        }

        void _model_OnGetExceptionHistoryCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.ExceptionLog> obj)
        {
            UserMessage = "Exception history loaded";
            PagedExceptionHistory = new PagedCollectionView(obj);
            PagedExceptionHistory.GroupDescriptions.Add(new PropertyGroupDescription("DateString"));
            PagedExceptionHistory.PageSize = PageSize;
        }

        void _model_OnGetLoginHistoryCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.LoginHistory> obj)
        {
            UserMessage = "Login history loaded";
            PagedLoginHistory = new PagedCollectionView(obj);
            PagedLoginHistory.GroupDescriptions.Add(new PropertyGroupDescription("LoginDateString"));
            PagedLoginHistory.PageSize = PageSize;
        }

        void _model_OnGetUserActivityCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.UserActivity> obj)
        {
            UserMessage = "User acitivity loaded";
            PagedUserActivityList = new PagedCollectionView(obj);
            PagedUserActivityList.GroupDescriptions.Add(new PropertyGroupDescription("DateString"));
            PagedUserActivityList.PageSize = PageSize;
        }

        void _model_OnServiceError(Entities.SMSException obj)
        {
            ErrorMessage = obj.Message;
        }

        private int _PageSize = 20;

        public int PageSize
        {
            get { return _PageSize; }
            set
            {
                _PageSize = value; OnPropertyChanged("PageSize");
                if (PagedUserActivityList != null)
                    PagedUserActivityList.PageSize = value;
                if (PagedLoginHistory != null)
                    PagedLoginHistory.PageSize = value;
                if (PagedExceptionHistory != null)
                    PagedExceptionHistory.PageSize = value;
            }
        }


        private DateTime _FromDate;

        public DateTime FromDate
        {
            get { return _FromDate; }
            set { _FromDate = value; OnPropertyChanged("ToDate"); }
        }

        private DateTime _ToDate;

        public DateTime ToDate
        {
            get { return _ToDate; }
            set { _ToDate = value; OnPropertyChanged("ToDate"); }
        }


        private PagedCollectionView _PagedUserActivityList;

        public PagedCollectionView PagedUserActivityList
        {
            get { return _PagedUserActivityList; }
            set { _PagedUserActivityList = value; OnPropertyChanged("PagedUserActivityList"); }
        }

        private PagedCollectionView _PagedLoginHistory;

        public PagedCollectionView PagedLoginHistory
        {
            get { return _PagedLoginHistory; }
            set { _PagedLoginHistory = value; OnPropertyChanged("PagedLoginHistory"); }
        }

        private PagedCollectionView _PagedExceptionHistory;

        public PagedCollectionView PagedExceptionHistory
        {
            get { return _PagedExceptionHistory; }
            set { _PagedExceptionHistory = value; OnPropertyChanged("PagedExceptionHistory"); }
        }


        public ICommand ShowCommand { get; private set; }

        public void ShowUserActivityCommand(object param)
        {
            if (Convert.ToString(param) == "Login")
                _model.GetLoginHistory(FromDate, ToDate);
            else if (Convert.ToString(param) == "Exception")
                _model.GetExceptionHistory(FromDate, ToDate);
            else
                _model.GetUserActivity(FromDate, ToDate);
            UserMessage = "Getting user activity...";
        }

    }
}
